//
//  Bonus_banner_View.m
//  MindBox
//
//  Created by MS on 16-1-2.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "Bonus_banner_View.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "UIView+Frame.h"
@interface Bonus_banner_View ()

@property (nonatomic,strong) NSMutableArray * labArr;
@property (nonatomic,strong) NSMutableArray * btnArr;

@end

@implementation Bonus_banner_View

-(NSMutableArray *)labArr
{
    if (_labArr==nil) {
        _labArr = [NSMutableArray array];
    }
    return _labArr;
}
-(NSMutableArray *)btnArr
{
    if (_btnArr==nil) {
        _btnArr = [NSMutableArray array];
    }
    return _btnArr;
}

-(void)setBonus_banner:(NSArray *)bonus_banner
{
    _bonus_banner = bonus_banner;
    
    for (int i =0; i<bonus_banner.count; i++) {
        Bonus_Banner * bonus =bonus_banner[i];
        UILabel * lab = self.labArr[i];
        [self.btnArr[i]  sd_setImageWithURL:[NSURL URLWithString:bonus.image_url] forState:UIControlStateNormal];
        [self.btnArr[i] setTitle:bonus.title forState:UIControlStateNormal];
        lab.text = bonus.title;
    }
   
}


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor =[UIColor whiteColor];
 
        for (int i =0;i<3;i++) {
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTintColor:[UIColor blackColor]];
            btn.tag = i;
            btn.highlighted = NO;
            [self.btnArr addObject:btn];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            
            UILabel * lab = [[UILabel alloc] init];
            lab.font = [UIFont systemFontOfSize:11];
            [self addSubview:lab];
            lab.textAlignment = NSTextAlignmentCenter;
            [self.labArr addObject:lab];
        }

        
    }
    return self;
}

-(void)btnClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(bonus_banner_DidClick:bonus_banner:index:)]) {
        
        [self.delegate bonus_banner_DidClick:self bonus_banner:self.bonus_banner[btn.tag] index:btn.tag];
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    int i = 0;
    NSInteger count = self.btnArr.count;
    CGFloat wh = 40;
    CGFloat margin =(self.width-count * wh)/(count+1);

    for (UILabel * lab in self.labArr) {
        CGFloat x = margin+(margin + wh)*i;
        UIButton * btn = self.btnArr[i];
        btn.frame = CGRectMake(x, 10, 40, 40);
        lab.frame =CGRectMake(0, 0, 100, 20);
        lab.center = CGPointMake(btn.center.x, CGRectGetMaxY(btn.frame)+10);
        i++;
    }

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
